<?php
set_time_limit(0);
require_once 'config.php';
require_once 'lib/xmlrpc.php';
require_once 'lib/amazon/amazon_api_class.php';

/*
 * Get Wordpress Category
*/
$wp_url = WP_URL . '/xmlrpc.php';
$wp_obj = new IXR_Client($wp_url);
$params = array(0,WP_USERNAME,WP_PASSWORD);
if (!$wp_obj->query('metaWeblog.getCategories', $params)) {
    $errMsg =  '***'.$wp_obj->getErrorCode().' : '.$wp_obj->getErrorMessage().'***';
}
$categories = $wp_obj->getResponse();

if($_POST['api1'] == 'LOAD') {
    $asin = trim($_POST['asin']);
    $keyword = trim($_POST['keyword']);
    try {
        $amazon_obj = new AmazonProductAPI();
        $amazon_obj->setAssociateTag(AWS_TAG);
        $amazon_obj->setPublicKey(AWS_PUBLIC_KEY);
        $amazon_obj->setPrivateKey(AWS_PRIVATE_KEY);
        $xml = $amazon_obj->getItemByAsin($asin);

        $item = $xml->Items->Item;
        $asin = $item->ASIN;
        $listPrice = $item->ItemAttributes->ListPrice->FormattedPrice;
        $price = $item->Offers->Offer->OfferListing->Price->FormattedPrice;
        $image = $item->MediumImage->URL;
        $url = 'http://www.amazon.com/dp/' . $asin . '?tag=' . AWS_TAG;
        $customer_review = 'http://www.amazon.com/product-reviews/'  . $asin .  '/?filterBy=addFiveStar&showViewpoints=0';

    }catch (Exception $e) {
        $errMsg = '***'.$e->getMessage().'***';
    }

}

if($_POST['api2'] == 'Get By Keyword') {
    $keyword = trim($_POST['keyword']);
    try {
        $amazon_obj = new AmazonProductAPI();
        $amazon_obj->setAssociateTag(AWS_TAG);
        $amazon_obj->setPublicKey(AWS_PUBLIC_KEY);
        $amazon_obj->setPrivateKey(AWS_PRIVATE_KEY);
        $xml = $amazon_obj->getItemByKeyword($keyword, 1);
        $awz_list = $xml->Items->Item;

    }catch (Exception $e) {
        $errMsg = '***'.$e->getMessage().'***';
    }
}

if($_POST['add'] == 'Post Data') {
    $asin = trim($_POST['asin']);
    $title = trim(stripslashes($_POST['wp_title']));
    $content = trim(stripslashes($_POST['wp_content']));
    $cat = trim($_POST['wp_category']);
    $new_cat = trim($_POST['wp_new_category']);
    $cats = $_POST['wp_categories'];
    $tags = trim($_POST['wp_tags']);
    $link = trim($_POST['link']);
    $image = trim($_POST['image']);
    $listprice = trim($_POST['listprice']);
    $price = $_POST['price'];

    $custom_fields = array(
            array( "key" => "amzn_LargeImageURL", "value" => $image ),
            array( "key" => "amzn_MediumImageURL", "value" => $image ),
            array( "key" => "link", "value" => $link ),
            array( "key" => "list_price", "value" => $listprice ),
            array( "key" => "price", "value" => $price )
                     );
   
    $data = array(
            'title'=>$title,
            'description'=> '<strong>Customer Review</strong><br>'.$content,
            'mt_allow_comments'=>0,  // 1 to allow comments
            'mt_allow_pings'=>0,  // 1 to allow trackbacks
            'post_type'=>'post',
            'mt_keywords'=>$tags,
            'categories'=>$cats,
            'custom_fields' => $custom_fields
    );
    $params = array(0,WP_USERNAME,WP_PASSWORD,$data,true);
    
    if (!$wp_obj->query('metaWeblog.newPost', $params)) {
        $errMsg = '***'.$client->getErrorCode().' : '.$client->getErrorMessage().'***';
    }else {
        $msg =  '***Posted '.$asin.' Successfully***';
    }

}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <form action="#" method="post">
            <div style="margin: 0 auto;width: 80%;color: red;text-align: center"><?=$errMsg?></div>
            <div style="margin: 0 auto;width: 80%;color: green;text-align: center"><?=$msg?></div>
            <fieldset style="background: #F2F2F2;"><legend><h3>Amazon Config</h3></legend>
                <table>
                    <!--
                    <tr>
                        <td>Associate Tag :</td>
                        <td><input type="text" id="asso" name="asso" size="50" /></td>
                    </tr>
                    <tr>
                        <td>Public Key :</td>
                        <td><input type="text" id="public" name="public" size="50" /></td>
                    </tr>
                    <tr>
                        <td>Private Key :</td>
                        <td><input type="text" id="private" name="private" size="50" /></td>
                    </tr> -->
                    
                    <?php if(count($awz_list) > 0) { ?>
                     <tr>
                        <td>Select Items :</td>
                        <td>
                            <input type="hidden" name="keyword" value="<?=$keyword?>" />
                            <select id="asin" name="asin">
                            <?php foreach ($awz_list as $val) { ?>
                                <option value="<?=$val->ASIN?>"><?=$val->ItemAttributes->Title?></option>
                            <?php } ?>
                            </select>
                            <input type="submit" name="api1" value="LOAD"
                        </td>
                    </tr>
                    <?php } else { ?>
                    <tr>
                        <td>Keyword :</td>
                        <td>
                            <input type="text" id="keyword" name="keyword" size="50" />
                            <input type="submit" name="api2" value="Get By Keyword"
                        </td>
                    </tr>
                    <tr>
                        <td>ASIN :</td>
                        <td>
                            <input type="text" id="asin" name="asin" size="50" />
                            <input type="submit" name="api1" value="LOAD"
                        </td>
                    </tr>
                    <?php } ?>
                </table>
            </fieldset>
            <?php if(count($item)> 0) { ?>
            <fieldset>
                <legend><h3>Wordpress Data</h3></legend>
                <table>

                    <tr>
                        <td>Title :</td>
                        <td>
                            <input type="hidden" id="asin" name="asin" value="<?=$asin?>" />
                            <input type="text" id="wp_title" name="wp_title" size="80" value="<?=$keyword?>" />
                        </td>
                    </tr>
                    <tr>
                        <td>Content :</td>
                        <td>
                            <textarea id="wp_content" name="wp_content" cols="100" rows="5"></textarea>
                            <div><a href="<?=$customer_review?>" target="_blank">Customer Review</a></div>
                            <div><a href="http://smallseotools.com/article-rewriter/" target="_blank">Rewrite</a></div>
                        </td>
                    </tr>
                    <tr>
                        <td>Category :</td>
                        <td>
                                <?php if(count($categories) > 0) { ?>
                                    <?php foreach ($categories as $cat) { ?>
                                        <?=$cat['categoryName']?>
                            <input type="checkbox" name="wp_categories[]" value="<?=$cat['categoryName']?>" />
                                        <?php } ?>
                            Other
                            <input type="checkbox" name="wp_category" value="other" />
                            <input type="text" id="wp_new_category" name="wp_new_category" size="10" />
                                    <?php }  ?>
                        </td>
                    </tr>
                    <tr>
                        <td>Tags :</td>
                        <td>
                            <input type="text" id="wp_tags" name="wp_tags" size="50" />
                            <i>ตัวอย่าง เช่น tag1, tag2</i>
                        </td>
                    </tr>
                    <tr>
                        <td>Link :</td>
                        <td>
                            <input type="text" id="link" name="link" value="<?=$url?>" size="80" />
                        </td>
                    </tr>
                    <tr>
                        <td>Image :</td>
                        <td>
                            <input type="text" id="image" name="image" value="<?=$image?>" size="80" />
                        </td>
                    </tr>
                    <tr>
                        <td>List Price :</td>
                        <td>
                            <input type="text" id="listprice" name="listprice" value="<?=$listPrice?>" size="10" />
                        </td>
                    </tr>
                    <tr>
                        <td>Price :</td>
                        <td>
                            <input type="text" id="price" name="price" value="<?=$price?>" size="10" />
                        </td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td>
                            <input type="submit" name="add" value="Post Data"  />
                        </td>
                    </tr>
                </table>
            </fieldset>
                <?php } ?>
        </form>
    </body>
</html>
